Component({
    properties: {
        offsetTop: {
            type: Number,
            value: 0
        },
        zIndex: {
            type: Number,
            value: 99
        },
        disabled: {
            type: Boolean,
            value: !1
        },
        container: {
            type: null
        },
        index: {
            type: Number,
            value: 0
        },
        sticky: {
            type: Boolean,
            value: !1
        }
    },
    data: {
        fixed: !1,
        height: 0,
        _attached: !1,
        _containerHeight: 0
    },
    lifetimes: {
        attached: function() {
            this.data._attached = !0, this.data.disabled || this.initObserver();
        },
        detached: function() {
            this.data._attached = !1, this.disconnectObserver();
        }
    },
    methods: {
        initObserver: function() {
            var e = this;
            this.disconnectObserver(), this.getRect(".weui-sticky").then(function(t) {
                e.setData({
                    height: t.height
                }), e.observerContent();
            });
        },
        observerContent: function() {
            var e = this, t = this.data.offsetTop;
            this.disconnectObserver("contentObserver");
            var i = this.createIntersectionObserver({
                thresholds: [ 0 ],
                initialRatio: 0
            });
            i.relativeToViewport({
                top: -t
            }), i.observe(".weui-sticky", function(t) {
                e.data.disabled || e.setFixed(t);
            }), this.contentObserver = i;
        },
        disconnectObserver: function(e) {
            if (e) {
                var t = this[e];
                t && t.disconnect();
            } else this.contentObserver && this.contentObserver.disconnect(), this.containerObserver && this.containerObserver.disconnect();
        },
        setFixed: function(e) {
            var t = e.boundingClientRect.top, i = this.data.offsetTop, n = !0;
            0 == e.intersectionRatio && (n = !1);
            var s = "up";
            t <= 0 || t > 0 && t <= i && !n || t > 0 && t < i || (s = "down"), this.triggerEvent("display", {
                index: this.data.index,
                display: n,
                type: s
            });
        },
        getRect: function(e) {
            var t = this;
            return new Promise(function(i, n) {
                t.createSelectorQuery().select(e).boundingClientRect(function(t) {
                    t ? i(t) : n(new Error("can not find selector: " + e));
                }).exec();
            });
        }
    }
});